/* ********************************************************************************************
*  Alvin Baldemeca
*  Tetris Project
*  Course: TCSS 305 , Alan Fowler
*  Term: Spring 2012
*  UNIVERISTY OF WASHINGTON TACOMA
*
*
**********************************************************************************************/
package gui.actions;


import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;


import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import view.TetrisBoardPanel;







/**
 * The action to be attached to the ellipse tool in the Power Paint program.
 * @author Alvin Baldemeca
 * @version 5/17/2012
 */
@SuppressWarnings("serial")
public class AssignKeyAction extends AbstractAction
{
  
  /**
   * The text field used to assign a keyboard control to.
   */
  private final JTextField my_text_field;
  
  /**
   * The panel that paints the tetris board.
   */
  private final TetrisBoardPanel my_panel;
    
  /**
   * The action to set to the a keyboard key.
   */
  private final Action my_action;
  
  
  /**
   * Creates a SetRotateAction.
   * @param the_text_field the text_field that contain the character string
   * @param the_panel the tetris panel that displays the board.
   * @param the_action the action to assign the keyboard keys to.
   */
  public AssignKeyAction(final JTextField the_text_field, 
                         final TetrisBoardPanel the_panel, final Action the_action) 
  {
    super((String) the_action.getValue(NAME));
    putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
    putValue(Action.SELECTED_KEY, true);
    my_text_field = the_text_field;
    my_panel = the_panel;
    my_action = the_action;
  }

  @Override
  public void actionPerformed(final ActionEvent the_event) 
  {
    final String key = my_text_field.getText().toUpperCase();
    my_panel.getInputMap().put(KeyStroke.getKeyStroke(key), my_action.toString());   
    my_panel.getActionMap().put(my_action.toString(), my_action); 
    my_panel.requestFocus();
    
  }
  

}
